define(function(require, exports, module) {

	var $ = require('jquery');
	var List = require('../util/list');
	require('artTemplate/artTemplate/2.0.1/template.js');
	require('seajs/seajs/2.1.1/sea.js');
	require('jquery/jquery-form/1.0.0/jquery.form.js');
	require('jquery/jquery-validation/1.11.0/jquery.validate');
	require('jquery/jquery-validation/1.11.0/messages_zh');
	require('jbox');
	
	exports.options = {
		listLink : '',
		listParams : '',
		pageLink : '',
		pageParams : ''
	};
	
	var allcategory = new Array();
	var name;
	var id;
	var category_code;
	var sort = 1;
	exports.showCategoryList = function showCategoryList(e) {
		var getting = $.get($CONFIG.base_url + "/api/system/project/categorylist",{type:e});
		getting.done(function(data) {
			allcategory = data;
			$.each(data,function(key,val){
				var trs = $("<tr><td width=20%>" + val.id + "</td>"+ 
								"<td width=40%>" + val.categoryName + "</td>" +
								"<td width=20%>" + val.projectAmount + "</td>" +
								"<td width=20%>" +
									'<div class="btn-group"> ' +
										'<button data=' + val.id + ' name=' + val.categoryName +' class="btn btn-warning" id="edit-category" data-target="#upModal" data-toggle="modal">' + '<i   class="icon-edit" ></i></a></button>' + 
						'<button id="del-catgory" class="btn btn-warning" data=' + val.id + ' ><i class="icon-trash" data=' + val.id +'></i> </button>' +
					'</div>' +
				'</td>');
				$("#proCategoryList tbody").append(trs);
			});
		});
	};
			
	$(document).ready(function(){
			$("#proCategoryList tbody").empty();
			exports.showCategoryList(3);		
	});
	$("#catgory").click(function(){
			$("#proCategoryList tbody").empty();
			exports.showCategoryList(3);
	});
	
	//添加项目类别
	$("#add-category").click(function(){

		$(".addto-address").show();
		$(".update-address").hide();
		
	});
	$("#upModal").on('click',".addto-address",function(){
		var category_name=$("input[id=categoryName]").val();
		if(exports.testExist(category_name)) {
			$.jBox.tip("分类名称已存在，请重新输入！");
			return
		}
		category_code = allcategory.length + 1;
		var postData = {"type":3,"categoryName":category_name,"categoryCode":category_code,"sort":sort};
		var addcategory = $.post($CONFIG.base_url + "/api/system/project/category/add",postData);
		
		addcategory.done(function(){
			$.jBox.tip("添加成功");
			$("#proCategoryList tbody").empty();
			exports.showCategoryList(3);	
			$("#upModal").modal("hide");
			})
		.fail(function(){
			alert("fail!");
			});
		
	});
	
	//修改项目类别
	$("#edit-category").live('click',function(){
		$(".addto-address").hide();
		$(".update-address").show();
		id = $(this).attr("data");
		name=$(this).attr("name");
		$("input[id=categoryName]").val(name);
		for(var i=0;i< allcategory.length; i++) {
			if(id==allcategory[i].id){
				category_code = allcategory[i].categoryCode;
				sort = allcategory[i].sort;
			}
		};
	});
	
	$("#upModal").on('click',".update-address",function(){
		var category_name=$("input[id=categoryName]").val();
		if(exports.testExist(category_name)) {
			$.jBox.tip("分类名称已存在，请重新输入！");
			return
		}
		var postData = {"id":id,"type":3,"categoryName":category_name,"categoryCode":category_code,"sort":sort};
		var addcategory = $.post($CONFIG.base_url + "/api/system/project/category/update",postData);
		
		addcategory.done(function(){
			$.jBox.tip("修改成功");
			$("#proCategoryList tbody").empty();
			exports.showCategoryList(3);	
			$("#upModal").modal("hide");
			})
		.fail(function(){
			alert("fail!");
			});
		
		
	});
	//检测新添加类别是否已存在
	exports.testExist = function testExist(str) {
		for(var i = 0;i < allcategory.length; i++) {
			if(allcategory[i].categoryName == str)
				return true	
		}
		return false
			
	};
	
	//删除类别
	$("#del-catgory").live('click',function(){
		id = $(this).attr("data");
		exports.del(id);
	});
	
	//删除函数
	exports.del=function(e){		
			exports.options.deleteCategoryLink=$CONFIG.base_url+"/api/system/project/category/delete";
			exports.options.deleteCategoryParams={"categoryId":e};
			$.jBox("删除后将无法恢复，确定删除该分类吗？", {title: "友情提示",buttons:{"确定":1,"取消":-1},submit:function(v,h,f){
				if(v=="1"){
					var delCategory=$.post(exports.options.deleteCategoryLink, exports.options.deleteCategoryParams);
					delCategory.done(function(data){
						if(data){
							$.jBox.tip("删除成功！");
							$("#proCategoryList tbody").empty();
							exports.showCategoryList(3);
						}else {
							$.jBox.tip("删除失败,不能删除该分类！");
							$("#proCategoryList tbody").empty();
							exports.showCategoryList(3);
						}
					});
					return true;
				}
				if(v=="-1"){
					$.jBox.close();
					return false;
				}
			}});
	};
});